﻿using Game2DFramework;
using Game2DFramework.Extensions;
using MechGame.Game.Particles;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MechGame.Game.Leveling
{
    enum AttachedItem
    {
        None,
        Energy,
        EnergyContainer,
    }

    class Box : GameObject
    {
        private readonly Texture2D _box;
        private Vector2 _position;
        private int _life;

        public bool Deleted { get { return _life == 0; } }
        public AttachedItem AttachedItem { get; private set; }

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; Bounds = new Rectangle((int) Position.X, (int) Position.Y, _box.Width, _box.Height); }
        }

        public Rectangle Bounds { get; private set; }

        public Box(Game2D game, AttachedItem attachedItem) : base(game)
        {
            _box = Game.Content.Load<Texture2D>("Textures/box");
            _life = 3;
            AttachedItem = attachedItem;
        }

        public override void ApplyDamage(int amount)
        {
            _life -= amount;
            if (_life <= 0)
            {
                _life = 0;
                Game.GetGlobalObject<ExplosionManager>("ExplosionManager").SpawnExplosion(Position + _box.GetCenter());

                if (AttachedItem != AttachedItem.None)
                {
                    var level = Game.GetGlobalObject<Level>("Level");
                    level.DropItem(AttachedItem, Position + _box.GetCenter());
                }
            } 
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (Deleted) return;
            spriteBatch.Draw(_box, Position, Color.White);
        }
    }
}
